<?php


namespace app\controller\api;


use app\BaseController;

class ImageController extends BaseController
{
    /**
     * 上传图片到本地
     * @return \think\response\Json
     */
    public function upload()
    {
        try {
            $file = $this->request->file('image');
            validate(['image' => 'fileExt:jpg,jpeg,png'])->check(['image' => $file]);
            $savename = \think\facade\Filesystem::disk("public")->putFile('payment', $file);
            $savename = str_replace('\\','/',$savename);
            return $this->success(['src' => '/storage/' . $savename]);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage(), $exception->getCode());
        }
    }

    public function upload111()
    {
        try {
            // 获取 base64 编码的图片数据
            $base64_image = $this->request->post('image');
            $arr = base64_image_upload($base64_image);
            // 返回上传后的图片路径
            return $this->success($arr['src']);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage(), $exception->getCode());
        }
    }
}